package org.dizena.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlUtil
{

    // 删除html标记
    public static String delHtmlTag(String htmlStr)
    {
        String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
        String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
        String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式

        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); // 过滤script标签

        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); // 过滤style标签

        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); // 过滤html标签

        return htmlStr.trim(); // 返回文本字符串
    }

    // 获取HTML中属性值
    public static String getAttrInHtml(String src, String key)
    {
        Pattern pattern = Pattern.compile("(?:" + key + "\\s*=\\s*)" + "['\"](.*?)['\"]");
        Matcher matcher = pattern.matcher(src);
        if (matcher.find())
        {
            return matcher.group().replaceAll(key + "\\s*=\\s*", "").replaceAll("\"", "");
        }
        return null;
    }

    // 截取数字
    public static Integer getNumbers(String content)
    {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find())
        {
            String res = matcher.group(0);
            return Integer.parseInt(res);
        }
        return null;
    }

    // 截取非数字
    public static String splitNotNumber(String content)
    {
        Pattern pattern = Pattern.compile("\\D+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find())
        {
            return matcher.group(0);
        }
        return "";
    }

    // 中文數字转阿拉伯数组【十万九千零六十 --> 109060】
    public static Integer chineseNumber2IntPlus(String chineseNumber)
    {
        try
        {
            char[] cnArr = new char[]{'一', '二', '三', '四', '五', '六', '七', '八', '九'};
            char[] chArr = new char[]{'十', '百', '千', '万', '亿'};

            int end = chineseNumber.indexOf("章");
            if (end == -1)
            {
                end = chineseNumber.indexOf("节");
            }
            if (end == -1)
            {
                return null;
            }
            String zStr = chineseNumber.substring(0, end);
            boolean is = false;
            for (char c : chArr)
            {
                if (zStr.contains(c + ""))
                {
                    is = true;
                    break;
                }
            }
            if (is)
            {
                return chineseNumber2Int(zStr);
            }
            else
            {
                String res = "";
                for (int i = 0; i < end; i++)
                {
                    char tmp = chineseNumber.charAt(i);
                    for (int j = 0; j < cnArr.length; j++)
                    {
                        if (cnArr[j] == tmp)
                        {
                            res += (j + 1) + "";
                        }
                        if ('两' == tmp)
                        {
                            res += 2 + "";
                        }
                    }
                }
                if (!"".equals(res))
                {
                    return Integer.parseInt(res);
                }

            }
        } catch (Exception e)
        {
        }
        return null;
    }

    // 中文數字转阿拉伯数组【十万九千零六十 --> 109060】
    public static Integer chineseNumber2Int(String chineseNumber)
    {
        Integer result = null;
        try
        {
            result = 0;
            int temp = 1;// 存放一个单位的数字如：十万
            int count = 0;// 判断是否有chArr
            char[] cnArr = new char[]{'一', '二', '三', '四', '五', '六', '七', '八', '九'};
            char[] chArr = new char[]{'十', '百', '千', '万', '亿'};
            for (int i = 0; i < chineseNumber.length(); i++)
            {
                boolean b = true;// 判断是否是chArr
                char c = chineseNumber.charAt(i);
                for (int j = 0; j < cnArr.length; j++)
                {// 非单位，即数字
                    if (c == cnArr[j])
                    {
                        if (0 != count)
                        {// 添加下一个单位之前，先把上一个单位值添加到结果中
                            result += temp;
                            temp = 1;
                            count = 0;
                        }
                        // 下标+1，就是对应的值
                        temp = j + 1;
                        b = false;
                        break;
                    }
                    if (c == '两')
                    {
                        // 下标+1，就是对应的值
                        temp = 2;
                        b = false;
                        break;
                    }
                }
                if (b)
                {// 单位{'十','百','千','万','亿'}
                    for (int j = 0; j < chArr.length; j++)
                    {
                        if (c == chArr[j])
                        {
                            switch (j)
                            {
                                case 0:
                                    temp *= 10;
                                    break;
                                case 1:
                                    temp *= 100;
                                    break;
                                case 2:
                                    temp *= 1000;
                                    break;
                                case 3:
                                    temp *= 10000;
                                    break;
                                case 4:
                                    temp *= 100000000;
                                    break;
                                default:
                                    break;
                            }
                            count++;
                        }
                    }
                }
                if (i == chineseNumber.length() - 1)
                {// 遍历到最后一个字符
                    result += temp;
                }
            }
            return result;
        } catch (Exception e)
        {
            return null;
        }

    }

    public static void main(String[] args)
    {
        System.out.println(chineseNumber2IntPlus("第两千零八章 他是暗王朝的人"));
    }

}
